<?php
class Core_Data_Mappers_Trainingzone extends Core_Data_Mappers_Abstract {
	protected $_businessclass = "Core_Business_Trainingzone";
	protected $_modelclass = "Core_Data_Models_Trainingzone";
	protected $_map=array("Id"=>"id","Trainingid"=>"trainingid","Codeinsee"=>"codeinsee");
	
	public function add($object) {
		if(!$this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$id = $this->getDbTable()->insert($data);
			$object->Id = $id;
		}
	}
	public function edit($object) {
		if($this->exists($object)) {
			$this->delete($object);
			$this->add($object);
		}
	}
	
	public function delete($object) {
		if($this->exists($object)) {
			$this->getDbTable()->delete(array("id = ?"=>$object->id));
		}
	}
	
	public function exists($object) {
		if($object->Id ==null)
		 return false;
		 else {
		$row = $this->getDbTable()->fetchRow(array("id = ?"=>$object->Id));
		if(isset($row) && count($row)==1) 
			return true;
		else
			return false;
		 }
	}
	
	public function flushtrainingzone($trainingid) {
		if(isset($trainingid))
			$this->getDbTable()->delete(array("trainingid=?"=>$trainingid));
	}
	
	public function find($id) {
		$row = $this->getDbTable()->fetchRow(array("id = ?"=>$id));
		if(isset($row) && count($row)==1) {
			$object = $this->map($row);
			return $object;
		}
		else 
			return null;
	}
	
	
}